/*
 * DB_InfoLoader.java
 *
 * Created on May 24, 2000, 12:38 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import java.util.*;

/**
 *
 * @author ayman
 */
public class MyDB_InfoLoader {
    
    
    private Vector db_names;
    private Vector dbObjectVector;
    private Vector tables_names;
    private Vector allFields_names = new Vector ();
    private Vector fields_names;
    
    
    /** Creates a new instance of DB_InfoLoader */
    public MyDB_InfoLoader() {
        getDB_Table_Field();
    }
    
    
     /**
     * This function load all the DBs and table names and the fields of the tables
     */
    private void getDB_Table_Field () {
        db_names= MainWindow.dbCon.getDatabases();
        MyDB_Object objs [] = new MyDB_Object [db_names.size()]; 
        
        
        for (int i =0 ; i < db_names.size() ; i++ ) {
            System.out.println(" data base num is " + db_names.size());
            objs[i]=new MyDB_Object ((String)db_names.elementAt(i));
            MainWindow.dbCon.setDatabase((String)db_names.elementAt(i));
            tables_names = MainWindow.dbCon.getTables();
            objs[i].setTablesName(tables_names);
            
            for (int j=0 ; j<tables_names.size() ; j++) {
                fields_names = MainWindow.dbCon.getFields((String)tables_names.elementAt(j));
                allFields_names.add(fields_names);
                //fields_names=null;
                System.out .println(" data base nu " + i + " tables nu is " + j);
            }
            objs[i].setFieldsNames(allFields_names);
            allFields_names=new Vector ();
        }
        
    }
    
    /**
     *  The function is used to get db names
     */
    public Vector getDB_Info () {
        return db_names;
    }
    
    /**
     *  The function takes db names and return table names
     */
    public Vector getDB_Info (String db_name) {
        Vector tablesNamesVector = new Vector ();
        for (int i=0; i <db_names.size() ; i++ ) {
            if (db_name == db_names.elementAt(i) ) {
                tablesNamesVector = ((MyDB_Object)dbObjectVector.elementAt(i)).getTablesNames();
                break;
            }
        }
        return tablesNamesVector;
    }
    
    /**
     *   The function takes db name and table name and return fields vector
     */
    public Vector getDB_Info (String db_name,String table_name) {
        Vector FieldsNamesVector = new Vector();
        Vector tablesNamesVector = new Vector ();
        MyDB_Object db = new MyDB_Object ();
        
        for (int i=0; i<db_names.size() ; i++ ){
            if (db_name == (String)db_names.elementAt(i)){
                db = (MyDB_Object)dbObjectVector.elementAt(i);
                tablesNamesVector = db.getTablesNames();
                break;
            }
        }
        for (int i=0 ; i<tablesNamesVector.size() ; i++) {
            if (table_name == (String)tablesNamesVector.elementAt(i)){
                FieldsNamesVector = db.getFieldsVector(table_name);
                break;
            }
        }
        
        return FieldsNamesVector;
    }
    
    
}
